package com.ruoyi.asset.recycle.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.asset.recycle.domain.bo.WasteRecycleBo;
import com.ruoyi.asset.recycle.domain.bo.WasteRecycleStatisticsBo;
import com.ruoyi.asset.recycle.domain.vo.RecycleCommunityCountVo;
import com.ruoyi.asset.recycle.domain.vo.RecycleWorkStatisticsVo;
import com.ruoyi.asset.recycle.domain.vo.WasteRecycleVo;
import com.ruoyi.asset.recycle.service.IWasteRecycleService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 垃圾收运
 *
 * @author 12276
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/asset/recycle")
public class WasteRecycleController extends BaseController {

    private final IWasteRecycleService recycleService;

    /**
     * 获取垃圾收运-管理范围导入模板
     */
    @SaIgnore()
    @Log(title = "垃圾收运范围导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping(value = "/importData")
    public R<List<String>> importData(@RequestParam(name = "excelUrl") String excelUrl) {
        List<String> list = this.recycleService.importExcel(excelUrl);

        if (list == null) {
            return R.fail();
        } else {
            return R.ok(list);
        }
    }

    /**
     * 获取垃圾收运-管理范围导入模板
     */
    @SaIgnore()
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "垃圾收运范围导入模板", WasteRecycleVo.class, response);
    }

    /**
     * 删除垃圾收运范围
     */
    @SaIgnore()
    @DeleteMapping("/deleteRecycle/{ids}")
    public R<Void> deleteToilet(@NotEmpty(message = "主键不能为空")
                                          @PathVariable Set<Long> ids) {
        int count = this.recycleService.deleteRecycle(ids);

        return toAjax(count >= 0 ? 1 : count);
    }

    /**
     * 查询垃圾收运范围
     */
    @SaIgnore()
    @GetMapping("/queryRecycle")
    public TableDataInfo<WasteRecycleVo> queryToilet(WasteRecycleBo searchBo, PageQuery pageQuery) {
        return this.recycleService.queryRecycle(searchBo, pageQuery);
    }

    /**
     * 导出垃圾收运范围
     */
    @SaIgnore()
    @Log(title = "垃圾收运导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportRecycle")
    public void exportToilet(WasteRecycleBo searchBo, HttpServletResponse response) {
        List<WasteRecycleVo> list = this.recycleService.exportRecycle(searchBo);

        ExcelUtil.exportExcel(list, "垃圾收运管理范围", WasteRecycleVo.class, response);
    }

    @SaIgnore()
    @GetMapping("/recycleCount")
    public R<WasteRecycleStatisticsBo> recycleCount() {
        WasteRecycleStatisticsBo recycleCount = recycleService.getRecycleCount();
        return R.ok(recycleCount);
    }

    @SaIgnore()
    @GetMapping("/recycleCommunityCount")
    public R<Map> recycleCommunityCount() {
        Map map = recycleService.getRecycleCommunityCount();
        return R.ok(map);
    }

    /**
     * 查询智慧大屏公园管养玉塘街道全社区
     *
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/recycleCommunity")
    public R<List<RecycleCommunityCountVo>> recycleCommunity(String date) {
        date = "2024-03-13";
        if (StringUtils.isEmpty(date)){
            return R.fail("查询失败，日期参数不能为空！");
        }
        List<RecycleCommunityCountVo> recycleCountByCommunity = recycleService.getRecycleCountByCommunity(date);
        return R.ok(recycleCountByCommunity);
    }

    /**
     * 查询智慧大屏垃圾分类督导今日作业统计与百分比
     *
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/recycleWorkByDate")
    public R<RecycleWorkStatisticsVo> recycleWorkByDate(String date) {
        date = "2024-03-13";
        if (StringUtils.isEmpty(date)){
            return R.fail("查询失败，日期参数不能为空！");
        }
        RecycleWorkStatisticsVo recycleWorkByDate = recycleService.getRecycleWorkByDate(date);
        return R.ok(recycleWorkByDate);
    }

}
